#include "dialog.h"
#include "ui_dialog.h"
#include "doexpr.h"
#include "check.h"
#include <QMessageBox>
#include <QString>

const double PI = 3.14159265358;

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)        //构造函数
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::CustomizeWindowHint|Qt::WindowCloseButtonHint); //去除窗口右上角“？”
    this->player->setMedia(QUrl("music/background.mp3"));
    this->player->setVolume(50);
    this->temp = "";
    this->doExpr = false;
    this->loopExpr = false;
}

Dialog::~Dialog()            //析构函数
{
    delete ui;
}

void Dialog::on_btn_0_clicked()
{
     //按钮0槽函数
    if(this->temp != "" && this->temp != "0")  //防止出现多位0
    {
        this->temp += this->ui->btn_0->text();
        if(!this->loopExpr)
            this->ui->lbl_display->setText(this->temp);  //判断是否已经进行过运算
        else
        {
            this->temp = "";                            //若已进行运算，将文本置空
            this->temp += this->ui->btn_0->text();
            this->ui->lbl_display->setText(this->temp);  //显示文本
            this->loopExpr = false;
        }
    }
    else     //若未输入其他按钮，只显示一个0
    {
        this->temp = "0";
        this->ui->lbl_display->setText(this->temp);
    }

}

void Dialog::on_btn_1_clicked()
{
    //按钮1槽函数
    this->temp += this->ui->btn_1->text();
    if(!this->loopExpr)
        this->ui->lbl_display->setText(this->temp);
    else
    {
        this->temp = "";
        this->temp += this->ui->btn_1->text();
        this->ui->lbl_display->setText(this->temp);
        this->loopExpr = false;
    }
}

void Dialog::on_btn_2_clicked()
{
    //按钮2槽函数
    this->temp += this->ui->btn_2->text();
    if(!this->loopExpr)
        this->ui->lbl_display->setText(this->temp);
    else
    {
        this->temp = "";
        this->temp += this->ui->btn_2->text();
        this->ui->lbl_display->setText(this->temp);
        this->loopExpr = false;
    }
}

void Dialog::on_btn_3_clicked()
{
    //按钮3槽函数
    this->temp += this->ui->btn_3->text();
    if(!this->loopExpr)
        this->ui->lbl_display->setText(this->temp);
    else
    {
        this->temp = "";
        this->temp += this->ui->btn_3->text();
        this->ui->lbl_display->setText(this->temp);
        this->loopExpr = false;
    }
}

void Dialog::on_btn_4_clicked()
{
    //按钮4槽函数
    this->temp += this->ui->btn_4->text();
    if(!this->loopExpr)
        this->ui->lbl_display->setText(this->temp);
    else
    {
        this->temp = "";
        this->temp += this->ui->btn_4->text();
        this->ui->lbl_display->setText(this->temp);
        this->loopExpr = false;
    }
}

void Dialog::on_btn_5_clicked()
{
    //按钮5槽函数
    this->temp += this->ui->btn_5->text();
    if(!this->loopExpr)
        this->ui->lbl_display->setText(this->temp);
    else
    {
        this->temp = "";
        this->temp += this->ui->btn_5->text();
        this->ui->lbl_display->setText(this->temp);
        this->loopExpr = false;
    }
}

void Dialog::on_btn_6_clicked()
{
    //按钮6槽函数
    this->temp += this->ui->btn_6->text();
    if(!this->loopExpr)
        this->ui->lbl_display->setText(this->temp);
    else
    {
        this->temp = "";
        this->temp += this->ui->btn_6->text();
        this->ui->lbl_display->setText(this->temp);
        this->loopExpr = false;
    }
}

void Dialog::on_btn_7_clicked()
{
    //按钮7槽函数
    this->temp += this->ui->btn_7->text();
    if(!this->loopExpr)
        this->ui->lbl_display->setText(this->temp);
    else
    {
        this->temp = "";
        this->temp += this->ui->btn_7->text();
        this->ui->lbl_display->setText(this->temp);
        this->loopExpr = false;
    }
}

void Dialog::on_btn_8_clicked()
{
    //按钮8槽函数
    this->temp += this->ui->btn_8->text();
    if(!this->loopExpr)
        this->ui->lbl_display->setText(this->temp);
    else
    {
        this->temp = "";
        this->temp += this->ui->btn_8->text();
        this->ui->lbl_display->setText(this->temp);
        this->loopExpr = false;
    }
}

void Dialog::on_btn_9_clicked()
{
    //按钮9槽函数
    this->temp += this->ui->btn_9->text();
    if(!this->loopExpr)
        this->ui->lbl_display->setText(this->temp);
    else
    {
        this->temp = "";
        this->temp += this->ui->btn_9->text();
        this->ui->lbl_display->setText(this->temp);
        this->loopExpr = false;
    }
}

void Dialog::on_btn_pi_clicked()
{
    //按钮pi槽函数
    this->temp += QString::number(PI,10,7);
    if(!this->loopExpr)
        this->ui->lbl_display->setText(this->temp);
    else
    {
        this->temp = "";
        this->temp += QString::number(PI,10,7);
        this->ui->lbl_display->setText(this->temp);
        this->loopExpr = false;
    }

}

void Dialog::on_btn_point_clicked()
{
    //按钮 "."槽函数
    if(this->temp == "")   //若直接按point，则加上前导0
        this->temp = "0";
    this->temp += this->ui->btn_point->text();
    if(!this->loopExpr)
        this->ui->lbl_display->setText(this->temp);
    else
    {
        this->temp = "0";
        this->temp += this->ui->btn_point->text();
        this->ui->lbl_display->setText(this->temp);
        this->loopExpr = false;
    }
}

void Dialog::on_btn_left_clicked()
{
    //左括号槽函数
    this->temp += this->ui->btn_left->text();
    if(!this->loopExpr)
        this->ui->lbl_display->setText(this->temp);
    else
    {
        this->temp = "";
        this->temp += this->ui->btn_left->text();
        this->ui->lbl_display->setText(this->temp);
        this->loopExpr = false;
    }
}

void Dialog::on_btn_right_clicked()
{
    //右括号槽函数
    this->temp += this->ui->btn_right->text();
    if(!this->loopExpr)
        this->ui->lbl_display->setText(this->temp);
    else
    {
        this->temp = "";
        this->temp += this->ui->btn_right->text();
        this->ui->lbl_display->setText(this->temp);
        this->loopExpr = false;
    }
}

void Dialog::on_btn_div_clicked()
{
    //除法槽函数
    this->temp += this->ui->btn_div->text();
    this->ui->lbl_display->setText(this->temp);
    if(this->doExpr)
        this->loopExpr = false;
}

void Dialog::on_btn_mul_clicked()
{
    //乘法槽函数
    this->temp += this->ui->btn_mul->text();
    this->ui->lbl_display->setText(this->temp);
    if(this->doExpr)
        this->loopExpr = false;
}

void Dialog::on_btn_sub_clicked()
{
    //减法槽函数
    this->temp += this->ui->btn_sub->text();
    this->ui->lbl_display->setText(this->temp);
    if(this->doExpr)
        this->loopExpr = false;
}

void Dialog::on_btn_add_clicked()
{
    //加法槽函数
    this->temp += this->ui->btn_add->text();
    this->ui->lbl_display->setText(this->temp);
    if(this->doExpr)
        this->loopExpr = false;
}

void Dialog::on_btn_clear_clicked()
{
    //CE槽函数
    this->temp = "";
    this->ui->lbl_display->setText("0");
}

void Dialog::on_btn_backspace_clicked()
{
    //退格键槽函数
    this->temp.chop(1);
    this->ui->lbl_display->setText(this->temp);
}

void Dialog::on_btn_reci_clicked()
{
    //求倒数槽函数，与等号相似
    if(!check(this->temp))     //判断表达式是否有误
    {
        QMessageBox::warning(NULL, "错误", "输入表达式有误！o((>ω< ))o");
    }
    else
    {
        if(!checkSignal(this->temp))   //判断是否存在正负号表达式
        {
            //若存在，则改变表达式使其符合算法
            //如（-5）-> （0-5）
            this->temp = insert_zero(this->temp);
        }
        if(!check(this->temp))   //再次检修改后查表达式是否正确
        {
            QMessageBox::warning(NULL, "错误", "输入表达式有误！o((>ω< ))o");
        }
        else
        {
            string reci = this->temp.toStdString();    //QString转为string
            reci = InfixToPostfi(reci);                //将中缀表达式转为后缀表达式
            double res_reci = postfixExpression(reci); //计算后缀表达式
            res_reci = 1 / res_reci;                   //取倒数
            if(res_reci != (long long)res_reci)        //判断是否为整型
                this->temp = QString::number(res_reci,10,7); //精度为小数点后7位，可调
            else
                this->temp = QString::number(res_reci,10,0);
            if(this->temp == "inf" ||this->temp == "nan")  //判断分母是否为0
            {
                QMessageBox::warning(NULL, "错误", "注意，分母不能为零哦！(ง •_•)ง");
                this->temp = "0";
            }
            this->ui->lbl_display->setText(this->temp);  //将结果显示到display
            this->doExpr = true;                         //记录已经进行运算
            this->loopExpr = true;
        }

    }
}

void Dialog::on_btn_equal_clicked()
{
    //等号槽函数，与求倒数相同，缺少求倒数过程
    if(!check(this->temp))
    {
        QMessageBox::warning(NULL, "错误", "注意，表达式输入有误！(＠_＠;)");
    }
    else
    {
        if(!checkSignal(this->temp))
        {
            this->temp = insert_zero(this->temp);
        }
        if(!check(this->temp))
        {
            QMessageBox::warning(NULL, "错误", "注意，表达式输入有误！(＠_＠;)");
        }
        else
        {
            string res = this->temp.toStdString();
            res = InfixToPostfi(res);
            double res_equal = postfixExpression(res);
            if(res_equal != (long long)res_equal)
                this->temp = QString::number(res_equal,10,7);
            else
                this->temp = QString::number(res_equal,10,0);
            if(this->temp == "inf" ||this->temp == "nan")
            {
                QMessageBox::warning(NULL, "错误", "注意，分母不能为零哦！(ง •_•)ง");
                this->temp = "0";
            }
            this->ui->lbl_display->setText(this->temp);
            this->doExpr = true;
            this->loopExpr = true;
        }
    }
}



void Dialog::on_btn_musicOn_clicked()
{
    //开启音乐槽函数
    //音乐结束后再次点击此按钮继续播放
    this->player->play();
}

void Dialog::on_btn_musicOff_clicked()
{
    //暂停音乐槽函数
    this->player->pause();
}




